package com.rui.proxy.model;

import com.rui.proxy.obj.TrainStation;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Description: 代售点卖票（CGLB代理类）
 * @Author: ZhangRui
 * @Date: 2023/3/2 15:46
 * @Version: 1.0
 */
public class CGLBProxyPoint implements MethodInterceptor {

    public TrainStation getProxyObject(Class<? extends TrainStation> trainStation) {
        //创建Enhancer对象，类似于JDK动态代理的Proxy类，下一步就是设置几个参数
        Enhancer enhancer = new Enhancer();
        //设置子类的字节码对象
        enhancer.setSuperclass(trainStation);
        //设置回调函数
        enhancer.setCallback(this);
        //创建代理对象并返回
        return (TrainStation) enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

        this.collectionCharge();

        return methodProxy.invokeSuper(o, objects);
    }


    /**
     * 代理类其他处理
     *
     * @return void
     * @author ZhangRui
     * @date 2023/3/2 15:26
     */
    private void collectionCharge() {
        System.out.println("代售加收5%手续费");
    }
}